(function () {
    angular.module('feibo.teacher.controller').controller('teacherContractGroupCtrl', teacherContractGroupCtrl);

    teacherContractGroupCtrl.$inject = ['$stateParams', 'TeacherService', 'kvMap', 'CommonService'];

    function teacherContractGroupCtrl($stateParams, TeacherService, kvMap, CommonService) {
        var vm = this;
        vm.data = {
            contractId: $stateParams.contractId,
            groupInfo: undefined,
            singleInfo: {},
            teacherList: [],
            searchData: {
                user_id: ''
            },
            httpGetTeacher: {
                name: '',
                employeeStatus: ''
            },
            httpPostData: {
                fb_shift_type_id: '',
                buyout_teacher_pack_id: ''
            },
            init: function () {
                this.teacherType = CommonService.cloneMapping(kvMap.teacherType, ['vkMap', 'common']);
                this.httpGetTeacher.employeeStatus = [kvMap.employeeStatus.EMPLOYEE].toString();
                return this;
            }
        }.init();
        vm.initData = function () {
            vm.getContractGroup();
            vm.JquEvent();
        };
        vm.JquEvent = function () {
            var list = $('#t_list li');
            if (list.lenght %2 != 0) {
               $('#t_list').addClass('nonde-border-botom');
            } else {
                $('#t_list').removeClass('nonde-border-botom');
            }
        }
        /*
         * ***************获取专属合同老师组*********************
         *         ****************************
         */
        vm.getContractGroup = function () {
            var data = vm.data;
            var queryConfig = {
                funcName: 'getContractGroup',
                container: data,
                propName: 'groupInfo',
                propChain: ['data'],
                showLoadLayer: false,
                urlPlaceholder: {
                    id: data.contractId
                }
            };
            TeacherService.getTeacherContract(queryConfig).then(function (returnedData) {
                if (returnedData.success) {
                    vm.data.contract_no = returnedData.data.contract_no;
                    vm.data.school_name = returnedData.data.school.name;
                }
            }).catch(function (error) {
                console.error(error);
            });
        };
        /*
         * ***************查询老师列表*********************
         *         ****************************
         */
        vm.fetchTeachers = function () {
            var querryConfig = {
                funcName: 'fetchTeachers',
                container: vm.data,
                propName: 'teacherList',
                propChain: ['data', 'entries'],
                showLoadLayer: false,
                httpGetParams: {}
            };
            Object.keys(vm.data.httpGetTeacher).forEach(function (item) {
                querryConfig.httpGetParams[item] = vm.data.httpGetTeacher[item];
            });
            TeacherService.fetchTeachers(querryConfig).then(function (resData) {
            })
        };
        vm.initPostData = function (group) {
            var shift_slot = group.shift_time_slot;
            vm.data.singleInfo = group;
            vm.data.httpPostData.fb_shift_type_id = group.fb_shift_type_id;
            vm.data.httpPostData.buyout_teacher_pack_id = group.id;
        };

        vm.addTeacher = function () {
            var postConfig = {
                funcName: 'addTeacher',
                successMsg: '添加买断老师成功',
                errorMsg: '添加买断老师失败',
                showLoadLayer: false,
                httpPostData: {
                    fb_shift_type_id: '',
                    buyout_teacher_pack_id: '',
                    teacher_id: vm.data.searchData.user_id
                }
            };
            Object.keys(vm.data.httpPostData).forEach(function (item) {
                postConfig.httpPostData[item] = vm.data.httpPostData[item];
            });
            TeacherService.addBuyTeacher(postConfig).then(function (data) {
                if (data.success) {
                    $('#addTeacher').modal('hide');
                    vm.getContractGroup();
                }
            })
        };
        vm.searchTeacher = {
            url: ENDPOINTS.teacher.fetchTeachers + '?employeeStatus=4&page_size=10&name={query}',
            container: vm.data.searchData,
            propName: 'user_id',
            propChain: ['data', 'entries'],
            resultsPropsConfig: {
                title: ['user.nickname', 'country'],
                value: ['user_id']
            }
        };

    }
}());
